import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:fusion_one/Utils/ossUtils.dart';
import 'package:fusion_one/authorization.dart';
import 'package:fusion_one/dtos/token.dart';
import 'package:fusion_one/http/request.dart';
import 'package:fusion_one/redux/states/index.dart';
import 'package:fusion_one/redux/store/index.dart';

import '../../redux/actions/tokenAction.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});
  @override
  State<LoginPage> createState() => _loginState();
}

class _viewModel {
  void Function(Token token) setToken;
  _viewModel({required this.setToken});
}

class _loginState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  final _usernameController = TextEditingController();
  final _passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('FusionOne 登录'),
        ),
        body: StoreConnector<AppState, _viewModel>(
          converter: (store) {
            return _viewModel(setToken: (Token token) {
              store.dispatch(SetTokenAction(token));
            });
          },
          builder: (context, vm) {
            return Container(
                padding: EdgeInsets.all(50),
                height: double.infinity,
                alignment: Alignment.center,
                child: Container(
                  alignment: Alignment.center,
                  width: 500,
                  child: Form(
                    key: _formKey,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        TextFormField(
                          // controller: _usernameController,
                          initialValue: '17684859750',
                          decoration: const InputDecoration(
                            labelText: '用户名',
                          ),
                          validator: (value) {
                            if (value == null || value.isEmpty) {
                              return '请输入用户名';
                            }
                            return null;
                          },
                        ),
                        TextFormField(
                          // 内容不可见
                          obscureText: true,
                          // controller: _passwordController,
                          initialValue: "123456",
                          decoration: const InputDecoration(
                            labelText: '密码',
                          ),
                          validator: (value) {
                            if (value == null || value.isEmpty) {
                              return '请输入密码';
                            }
                            return null;
                          },
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 50),
                          width: double.infinity,
                          child: ElevatedButton(
                            onPressed: () async {
                              if (_formKey.currentState!.validate()) {
                                var username = '17684859750';
                                var password = '123456';
                                var token = await getToken(
                                    username: username, password: password);
                                vm.setToken(token);

                                await OSSUtils.init();
                                Navigator.pushNamed(context, '/');
                              }
                            },
                            child: const Text('登录'),
                          ),
                        )
                      ],
                    ),
                  ),
                ));
          },
        ));
  }
}
